import logging
import time
import os
from broadcast_listener import satellite_simulator
from command_recevier import tcp_recevier
from multiprocessing import Process
from tools import get_ip_address
from data_tranform_server import start_tcp_server,start_udp_server

logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S',
                    filename='log.log',
                    filemode='w')

if __name__ == "__main__":
    broad_port = os.getenv("BROAD_PORT", None)
    if broad_port is None:
        logging.error('No broadcast port information')
        raise Exception('No broadcast port information')
    broad_port = int(broad_port)
    host_ip = os.getenv("HOST_IP", None)
    threshold_str = os.getenv("THRESHOLD", '0.95')
    threshold = float(threshold_str)
    if host_ip is None:
        raise Exception('No host ip information')
    node_id = os.getenv("NODE_ID", None)
    if node_id is None:
        logging.error('No node id information')
        raise Exception('No node id information')
    procs = [
        Process(target=satellite_simulator, args=[broad_port, node_id, threshold]),
        Process(target=tcp_recevier, args=[get_ip_address()]),
        Process(target=start_tcp_server),
        Process(target=start_udp_server)
    ]
    for sub_proc in procs:
        sub_proc.start()
    while True:
        for i in range(len(procs)):
            pass
        time.sleep(60)
